<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>channelrequest_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_net_webchannel_channelrequest_test.js.html">channelrequest_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.labs.net.webChannel.ChannelRequest.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.labs.net.webChannel.channelRequestTest&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line24"></a>goog.require(&#39;goog.functions&#39;);
<a name="line25"></a>goog.require(&#39;goog.labs.net.webChannel.ChannelRequest&#39;);
<a name="line26"></a>goog.require(&#39;goog.labs.net.webChannel.WebChannelDebug&#39;);
<a name="line27"></a>goog.require(&#39;goog.labs.net.webChannel.requestStats&#39;);
<a name="line28"></a>goog.require(&#39;goog.labs.net.webChannel.requestStats.ServerReachability&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line31"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line32"></a>goog.require(&#39;goog.testing.net.XhrIo&#39;);
<a name="line33"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line34"></a>
<a name="line35"></a>goog.setTestOnly(&#39;goog.labs.net.webChannel.channelRequestTest&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>var channelRequest;
<a name="line39"></a>var mockChannel;
<a name="line40"></a>var mockClock;
<a name="line41"></a>var stubs;
<a name="line42"></a>var xhrIo;
<a name="line43"></a>var reachabilityEvents;
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Time to wait for a network request to time out, before aborting.
<a name="line48"></a> */
<a name="line49"></a>var WATCHDOG_TIME = 2000;
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * Time to throttle readystatechange events.
<a name="line54"></a> */
<a name="line55"></a>var THROTTLE_TIME = 500;
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * A really long time - used to make sure no more timeouts will fire.
<a name="line60"></a> */
<a name="line61"></a>var ALL_DAY_MS = 1000 * 60 * 60 * 24;
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>function setUp() {
<a name="line65"></a>  mockClock = new goog.testing.MockClock();
<a name="line66"></a>  mockClock.install();
<a name="line67"></a>  reachabilityEvents = {};
<a name="line68"></a>  stubs = new goog.testing.PropertyReplacer();
<a name="line69"></a>
<a name="line70"></a>  // Mock out the stat notification code.
<a name="line71"></a>  var notifyServerReachabilityEvent = function(reachabilityType) {
<a name="line72"></a>    if (!reachabilityEvents[reachabilityType]) {
<a name="line73"></a>      reachabilityEvents[reachabilityType] = 0;
<a name="line74"></a>    }
<a name="line75"></a>    reachabilityEvents[reachabilityType]++;
<a name="line76"></a>  };
<a name="line77"></a>  stubs.set(goog.labs.net.webChannel.requestStats,
<a name="line78"></a>      &#39;notifyServerReachabilityEvent&#39;, notifyServerReachabilityEvent);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>function tearDown() {
<a name="line83"></a>  stubs.reset();
<a name="line84"></a>  mockClock.uninstall();
<a name="line85"></a>}
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Constructs a duck-type WebChannelBase that tracks the completed requests.
<a name="line91"></a> * @constructor
<a name="line92"></a> * @struct
<a name="line93"></a> * @final
<a name="line94"></a> */
<a name="line95"></a>function MockWebChannelBase() {
<a name="line96"></a>  this.isClosed = function() {
<a name="line97"></a>    return false;
<a name="line98"></a>  };
<a name="line99"></a>  this.isActive = function() {
<a name="line100"></a>    return true;
<a name="line101"></a>  };
<a name="line102"></a>  this.shouldUseSecondaryDomains = function() {
<a name="line103"></a>    return false;
<a name="line104"></a>  };
<a name="line105"></a>  this.completedRequests = [];
<a name="line106"></a>  this.onRequestComplete = function(request) {
<a name="line107"></a>    this.completedRequests.push(request);
<a name="line108"></a>  };
<a name="line109"></a>  this.onRequestData = function(request, data) {};
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Creates a real ChannelRequest object, with some modifications for
<a name="line115"></a> * testability:
<a name="line116"></a> * &lt;ul&gt;
<a name="line117"></a> * &lt;li&gt;The channel is a mock channel.
<a name="line118"></a> * &lt;li&gt;The new watchdogTimeoutCallCount property tracks onWatchDogTimeout_()
<a name="line119"></a> *     calls.
<a name="line120"></a> * &lt;li&gt;The timeout is set to WATCHDOG_TIME.
<a name="line121"></a> * &lt;/ul&gt;
<a name="line122"></a> */
<a name="line123"></a>function createChannelRequest() {
<a name="line124"></a>  xhrIo = new goog.testing.net.XhrIo();
<a name="line125"></a>  xhrIo.abort = xhrIo.abort || function() {
<a name="line126"></a>    this.active_ = false;
<a name="line127"></a>  };
<a name="line128"></a>
<a name="line129"></a>  // Install mock channel and no-op debug logger.
<a name="line130"></a>  mockChannel = new MockWebChannelBase();
<a name="line131"></a>  channelRequest = new goog.labs.net.webChannel.ChannelRequest(
<a name="line132"></a>      mockChannel,
<a name="line133"></a>      new goog.labs.net.webChannel.WebChannelDebug());
<a name="line134"></a>
<a name="line135"></a>  // Install test XhrIo.
<a name="line136"></a>  mockChannel.createXhrIo = function() {
<a name="line137"></a>    return xhrIo;
<a name="line138"></a>  };
<a name="line139"></a>
<a name="line140"></a>  // Install watchdogTimeoutCallCount.
<a name="line141"></a>  channelRequest.watchdogTimeoutCallCount = 0;
<a name="line142"></a>  channelRequest.originalOnWatchDogTimeout = channelRequest.onWatchDogTimeout_;
<a name="line143"></a>  channelRequest.onWatchDogTimeout_ = function() {
<a name="line144"></a>    channelRequest.watchdogTimeoutCallCount++;
<a name="line145"></a>    return channelRequest.originalOnWatchDogTimeout();
<a name="line146"></a>  };
<a name="line147"></a>
<a name="line148"></a>  channelRequest.setTimeout(WATCHDOG_TIME);
<a name="line149"></a>}
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Run through the lifecycle of a long lived request, checking that the right
<a name="line154"></a> * network events are reported.
<a name="line155"></a> */
<a name="line156"></a>function testNetworkEvents() {
<a name="line157"></a>  createChannelRequest();
<a name="line158"></a>
<a name="line159"></a>  channelRequest.xmlHttpPost(new goog.Uri(&#39;some_uri&#39;), &#39;some_postdata&#39;, true);
<a name="line160"></a>  checkReachabilityEvents(1, 0, 0, 0);
<a name="line161"></a>  if (goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming()) {
<a name="line162"></a>    xhrIo.simulatePartialResponse(&#39;17\nI am a BC Message&#39;);
<a name="line163"></a>    checkReachabilityEvents(1, 0, 0, 1);
<a name="line164"></a>    xhrIo.simulatePartialResponse(&#39;23\nI am another BC Message&#39;);
<a name="line165"></a>    checkReachabilityEvents(1, 0, 0, 2);
<a name="line166"></a>    xhrIo.simulateResponse(200, &#39;16\Final BC Message&#39;);
<a name="line167"></a>    checkReachabilityEvents(1, 1, 0, 2);
<a name="line168"></a>  } else {
<a name="line169"></a>    xhrIo.simulateResponse(200, &#39;16\Final BC Message&#39;);
<a name="line170"></a>    checkReachabilityEvents(1, 1, 0, 0);
<a name="line171"></a>  }
<a name="line172"></a>}
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Test throttling of readystatechange events.
<a name="line177"></a> */
<a name="line178"></a>function testNetworkEvents_throttleReadyStateChange() {
<a name="line179"></a>  createChannelRequest();
<a name="line180"></a>  channelRequest.setReadyStateChangeThrottle(THROTTLE_TIME);
<a name="line181"></a>
<a name="line182"></a>  var recordedHandler =
<a name="line183"></a>      goog.testing.recordFunction(channelRequest.xmlHttpHandler_);
<a name="line184"></a>  stubs.set(channelRequest, &#39;xmlHttpHandler_&#39;, recordedHandler);
<a name="line185"></a>
<a name="line186"></a>  channelRequest.xmlHttpPost(new goog.Uri(&#39;some_uri&#39;), &#39;some_postdata&#39;, true);
<a name="line187"></a>  assertEquals(1, recordedHandler.getCallCount());
<a name="line188"></a>
<a name="line189"></a>  checkReachabilityEvents(1, 0, 0, 0);
<a name="line190"></a>  if (goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming()) {
<a name="line191"></a>    xhrIo.simulatePartialResponse(&#39;17\nI am a BC Message&#39;);
<a name="line192"></a>    checkReachabilityEvents(1, 0, 0, 1);
<a name="line193"></a>    assertEquals(3, recordedHandler.getCallCount());
<a name="line194"></a>
<a name="line195"></a>    // Second event should be throttled
<a name="line196"></a>    xhrIo.simulatePartialResponse(&#39;23\nI am another BC Message&#39;);
<a name="line197"></a>    assertEquals(3, recordedHandler.getCallCount());
<a name="line198"></a>
<a name="line199"></a>    xhrIo.simulatePartialResponse(&#39;27\nI am yet another BC Message&#39;);
<a name="line200"></a>    assertEquals(3, recordedHandler.getCallCount());
<a name="line201"></a>    mockClock.tick(THROTTLE_TIME);
<a name="line202"></a>
<a name="line203"></a>    checkReachabilityEvents(1, 0, 0, 3);
<a name="line204"></a>    // Only one more call because of throttling.
<a name="line205"></a>    assertEquals(4, recordedHandler.getCallCount());
<a name="line206"></a>
<a name="line207"></a>    xhrIo.simulateResponse(200, &#39;16\Final BC Message&#39;);
<a name="line208"></a>    checkReachabilityEvents(1, 1, 0, 3);
<a name="line209"></a>    assertEquals(5, recordedHandler.getCallCount());
<a name="line210"></a>  } else {
<a name="line211"></a>    xhrIo.simulateResponse(200, &#39;16\Final BC Message&#39;);
<a name="line212"></a>    checkReachabilityEvents(1, 1, 0, 0);
<a name="line213"></a>  }
<a name="line214"></a>}
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * Make sure that the request &quot;completes&quot; with an error when the timeout
<a name="line219"></a> * expires.
<a name="line220"></a> */
<a name="line221"></a>function testRequestTimeout() {
<a name="line222"></a>  createChannelRequest();
<a name="line223"></a>
<a name="line224"></a>  channelRequest.xmlHttpPost(new goog.Uri(&#39;some_uri&#39;), &#39;some_postdata&#39;, true);
<a name="line225"></a>  assertEquals(0, channelRequest.watchdogTimeoutCallCount);
<a name="line226"></a>  assertEquals(0, channelRequest.channel_.completedRequests.length);
<a name="line227"></a>
<a name="line228"></a>  // Watchdog timeout.
<a name="line229"></a>  mockClock.tick(WATCHDOG_TIME);
<a name="line230"></a>  assertEquals(1, channelRequest.watchdogTimeoutCallCount);
<a name="line231"></a>  assertEquals(1, channelRequest.channel_.completedRequests.length);
<a name="line232"></a>  assertFalse(channelRequest.getSuccess());
<a name="line233"></a>
<a name="line234"></a>  // Make sure no more timers are firing.
<a name="line235"></a>  mockClock.tick(ALL_DAY_MS);
<a name="line236"></a>  assertEquals(1, channelRequest.watchdogTimeoutCallCount);
<a name="line237"></a>  assertEquals(1, channelRequest.channel_.completedRequests.length);
<a name="line238"></a>
<a name="line239"></a>  checkReachabilityEvents(1, 0, 1, 0);
<a name="line240"></a>}
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>function testRequestTimeoutWithUnexpectedException() {
<a name="line244"></a>  createChannelRequest();
<a name="line245"></a>  channelRequest.channel_.createXhrIo = goog.functions.error(&#39;Weird error&#39;);
<a name="line246"></a>
<a name="line247"></a>  try {
<a name="line248"></a>    channelRequest.xmlHttpGet(new goog.Uri(&#39;some_uri&#39;), true, null);
<a name="line249"></a>    fail(&#39;Expected error&#39;);
<a name="line250"></a>  } catch (e) {
<a name="line251"></a>    assertEquals(&#39;Weird error&#39;, e.message);
<a name="line252"></a>  }
<a name="line253"></a>
<a name="line254"></a>  assertEquals(0, channelRequest.watchdogTimeoutCallCount);
<a name="line255"></a>  assertEquals(0, channelRequest.channel_.completedRequests.length);
<a name="line256"></a>
<a name="line257"></a>  // Watchdog timeout.
<a name="line258"></a>  mockClock.tick(WATCHDOG_TIME);
<a name="line259"></a>  assertEquals(1, channelRequest.watchdogTimeoutCallCount);
<a name="line260"></a>  assertEquals(1, channelRequest.channel_.completedRequests.length);
<a name="line261"></a>  assertFalse(channelRequest.getSuccess());
<a name="line262"></a>
<a name="line263"></a>  // Make sure no more timers are firing.
<a name="line264"></a>  mockClock.tick(ALL_DAY_MS);
<a name="line265"></a>  assertEquals(1, channelRequest.watchdogTimeoutCallCount);
<a name="line266"></a>  assertEquals(1, channelRequest.channel_.completedRequests.length);
<a name="line267"></a>
<a name="line268"></a>  checkReachabilityEvents(0, 0, 1, 0);
<a name="line269"></a>}
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>function testActiveXBlocked() {
<a name="line273"></a>  createChannelRequest();
<a name="line274"></a>  stubs.set(goog.global, &#39;ActiveXObject&#39;,
<a name="line275"></a>      goog.functions.error(&#39;Active X blocked&#39;));
<a name="line276"></a>
<a name="line277"></a>  channelRequest.tridentGet(new goog.Uri(&#39;some_uri&#39;), false);
<a name="line278"></a>  assertFalse(channelRequest.getSuccess());
<a name="line279"></a>  assertEquals(
<a name="line280"></a>      goog.labs.net.webChannel.ChannelRequest.Error.ACTIVE_X_BLOCKED,
<a name="line281"></a>      channelRequest.getLastError());
<a name="line282"></a>
<a name="line283"></a>  checkReachabilityEvents(0, 0, 0, 0);
<a name="line284"></a>}
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>function checkReachabilityEvents(reqMade, reqSucceeded, reqFail, backChannel) {
<a name="line288"></a>  var Reachability =
<a name="line289"></a>      goog.labs.net.webChannel.requestStats.ServerReachability;
<a name="line290"></a>  assertEquals(reqMade,
<a name="line291"></a>      reachabilityEvents[Reachability.REQUEST_MADE] || 0);
<a name="line292"></a>  assertEquals(reqSucceeded,
<a name="line293"></a>      reachabilityEvents[Reachability.REQUEST_SUCCEEDED] || 0);
<a name="line294"></a>  assertEquals(reqFail,
<a name="line295"></a>      reachabilityEvents[Reachability.REQUEST_FAILED] || 0);
<a name="line296"></a>  assertEquals(backChannel,
<a name="line297"></a>      reachabilityEvents[Reachability.BACK_CHANNEL_ACTIVITY] || 0);
<a name="line298"></a>}
<a name="line299"></a>
<a name="line300"></a>
<a name="line301"></a>function testDuplicatedRandomParams() {
<a name="line302"></a>  createChannelRequest();
<a name="line303"></a>  channelRequest.xmlHttpGet(new goog.Uri(&#39;some_uri&#39;), true, null, true,
<a name="line304"></a>      true /* opt_duplicateRandom */);
<a name="line305"></a>  var z = xhrIo.getLastUri().getParameterValue(&#39;zx&#39;);
<a name="line306"></a>  var z1 = xhrIo.getLastUri().getParameterValue(&#39;zx1&#39;);
<a name="line307"></a>  assertTrue(goog.isDefAndNotNull(z));
<a name="line308"></a>  assertTrue(goog.isDefAndNotNull(z1));
<a name="line309"></a>  assertEquals(z1, z);
<a name="line310"></a>}
</pre>


</body>
</html>
